//
//  PhiCung.m
//  PhongThuyLibrary
//
//  Created by Le Anh Tai on 11/9/13.
//  Copyright (c) 2013 Phuong Le. All rights reserved.
//

#import "PhiCung.h"
#import "FengshuiDefines.h"

@implementation PhiCung

- (id)initWithCung:(NSString *)cung
{
    self = [super init];
    if (self)
    {
        self.cung = cung;
    }

    return self;
}

- (void)setCung:(NSString *)cung
{
    _cung = cung;
    _index = [self calculateIndexOfCung:_cung];
}

- (NSInteger)calculateIndexOfCung:(NSString*)cung
{
    NSInteger quaiSoIdx = [BAT_CUNG indexOfObject:cung];
    
    if (quaiSoIdx < 0 || quaiSoIdx >= BAT_CUNG.count)
    {
        return -1;
    }
    
    return quaiSoIdx;
}

- (NSString *)getThuocTuTrach
{
    if (_cung == nil) return nil;
    
    if ([DONG_TU_TRACH indexOfObject:_cung] != NSNotFound)
    {
        return TU_TRACH_DONG;
    }
    
    if ([TAY_TU_TRACH indexOfObject:_cung] != NSNotFound)
    {
        return TU_TRACH_TAY;
    }
    
    return nil;
}

- (NSString *)getThuocHanh
{
    if (_cung == nil)
    {
        return nil;
    }
    
    if ([_cung isEqualToString:CUNG_PHI_CAN2]) return HANH_KIM;
    if ([_cung isEqualToString:CUNG_PHI_KHAM]) return HANH_THUY;
    if ([_cung isEqualToString:CUNG_PHI_CAN1]) return HANH_THO;
    if ([_cung isEqualToString:CUNG_PHI_CHAN]) return HANH_MOC;
    if ([_cung isEqualToString:CUNG_PHI_TON]) return HANH_MOC;
    if ([_cung isEqualToString:CUNG_PHI_LY]) return HANH_HOA;
    if ([_cung isEqualToString:CUNG_PHI_KHON]) return HANH_THO;
    if ([_cung isEqualToString:CUNG_PHI_DOAI]) return HANH_KIM;
    return nil;
}

- (NSString *)getThuocAmDuong
{
    NSInteger index = [CUNG_PHI_VI_DUONG indexOfObject:_cung];
    if (index != NSNotFound)
    {
        return VI_DUONG;
    }
    
    index = [CUNG_PHI_VI_AM indexOfObject:_cung];
    if (index != NSNotFound)
    {
        return VI_AM;
    }
    
    return @"";
}

- (NSString *)getKietHungWithCung:(NSString *)cung
{
    int index = [self indexKietHungWithCung:cung];
    
    if (index < 0 || index >= BAT_TRACH_HUNG_KIET.count)
    {
        return nil;
    }
    
    return [BAT_TRACH_HUNG_KIET objectAtIndex:index];
}

- (int)indexKietHungWithCung:(NSString *)cungB
{
    int index = [self calculateIndexOfCung:_cung];
    if (index == -1) return -1;
    
    NSArray *cungHungKiet = [MAXTRIX_HUNG_KIET objectAtIndex:index];
    
    int indexCungB = [cungHungKiet indexOfObject:cungB];
    
    return indexCungB;
}

@end
